#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;
/*
TATGATAC
TAAGCTAC
AAAGATCC
TGAGATAC
TAAGATGT
*/
int n, m;
string s[55];
int cnt[128];
void solve() {
  cin >> n >> m;
  rep(i, 1, n) cin >> s[i];
  string ans;
  int d = 0;
  rep(j, 0, m - 1) {
    rep(i, 'A', 'Z') cnt[i] = 0;
    char mx = 'A';
    rep(i, 1, n) {
      char c = s[i][j];
      cnt[c]++;
      if (cnt[c] > cnt[mx] || (cnt[c] == cnt[mx] && c < mx)) mx = c;
    }
    d += n - cnt[mx];
    ans.push_back(mx);
  }
  cout << ans << endl;
  cout << d << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}